<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.genfromtxt &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Input and output" href="../routines.io.html" >
    <link rel="next" title="numpy.fromregex" href="numpy.fromregex.html" >
    <link rel="prev" title="numpy.savetxt" href="numpy.savetxt.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.io.html" accesskey="U">Input and output</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.fromregex.html" title="numpy.fromregex"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.savetxt.html" title="numpy.savetxt"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.savetxt.html"
                        title="previous chapter">numpy.savetxt</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.fromregex.html"
                        title="next chapter">numpy.fromregex</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-genfromtxt">
<h1>numpy.genfromtxt<a class="headerlink" href="#numpy-genfromtxt" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.genfromtxt">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">genfromtxt</code><span class="sig-paren">(</span><em class="sig-param">fname</em>, <em class="sig-param">dtype=&lt;class 'float'&gt;</em>, <em class="sig-param">comments='#'</em>, <em class="sig-param">delimiter=None</em>, <em class="sig-param">skip_header=0</em>, <em class="sig-param">skip_footer=0</em>, <em class="sig-param">converters=None</em>, <em class="sig-param">missing_values=None</em>, <em class="sig-param">filling_values=None</em>, <em class="sig-param">usecols=None</em>, <em class="sig-param">names=None</em>, <em class="sig-param">excludelist=None</em>, <em class="sig-param">deletechars=&quot; !#$%&amp;'()*+</em>, <em class="sig-param">-./:;&lt;=&gt;?&#64;[\]^{|}~&quot;</em>, <em class="sig-param">replace_space='_'</em>, <em class="sig-param">autostrip=False</em>, <em class="sig-param">case_sensitive=True</em>, <em class="sig-param">defaultfmt='f%i'</em>, <em class="sig-param">unpack=None</em>, <em class="sig-param">usemask=False</em>, <em class="sig-param">loose=True</em>, <em class="sig-param">invalid_raise=True</em>, <em class="sig-param">max_rows=None</em>, <em class="sig-param">encoding='bytes'</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/lib/npyio.py#L1561-L2254"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.genfromtxt" title="Permalink to this definition">¶</a></dt>
<dd><p>Load data from a text file, with missing values handled as specified.</p>
<p>Each line past the first <em class="xref py py-obj">skip_header</em> lines is split at the <em class="xref py py-obj">delimiter</em>
character, and characters following the <em class="xref py py-obj">comments</em> character are discarded.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>fname</strong><span class="classifier">file, str, pathlib.Path, list of str, generator</span></dt><dd><p>File, filename, list, or generator to read.  If the filename
extension is <em class="xref py py-obj">gz</em> or <a class="reference external" href="https://docs.python.org/dev/library/bz2.html#module-bz2" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">bz2</span></code></a>, the file is first decompressed. Note
that generators must return byte strings. The strings
in a list or produced by a generator are treated as lines.</p>
</dd>
<dt><strong>dtype</strong><span class="classifier">dtype, optional</span></dt><dd><p>Data type of the resulting array.
If None, the dtypes will be determined by the contents of each
column, individually.</p>
</dd>
<dt><strong>comments</strong><span class="classifier">str, optional</span></dt><dd><p>The character used to indicate the start of a comment.
All the characters occurring on a line after a comment are discarded</p>
</dd>
<dt><strong>delimiter</strong><span class="classifier">str, int, or sequence, optional</span></dt><dd><p>The string used to separate values.  By default, any consecutive
whitespaces act as delimiter.  An integer or sequence of integers
can also be provided as width(s) of each field.</p>
</dd>
<dt><strong>skiprows</strong><span class="classifier">int, optional</span></dt><dd><p><em class="xref py py-obj">skiprows</em> was removed in numpy 1.10. Please use <em class="xref py py-obj">skip_header</em> instead.</p>
</dd>
<dt><strong>skip_header</strong><span class="classifier">int, optional</span></dt><dd><p>The number of lines to skip at the beginning of the file.</p>
</dd>
<dt><strong>skip_footer</strong><span class="classifier">int, optional</span></dt><dd><p>The number of lines to skip at the end of the file.</p>
</dd>
<dt><strong>converters</strong><span class="classifier">variable, optional</span></dt><dd><p>The set of functions that convert the data of a column to a value.
The converters can also be used to provide a default value
for missing data: <code class="docutils literal notranslate"><span class="pre">converters</span> <span class="pre">=</span> <span class="pre">{3:</span> <span class="pre">lambda</span> <span class="pre">s:</span> <span class="pre">float(s</span> <span class="pre">or</span> <span class="pre">0)}</span></code>.</p>
</dd>
<dt><strong>missing</strong><span class="classifier">variable, optional</span></dt><dd><p><em class="xref py py-obj">missing</em> was removed in numpy 1.10. Please use <em class="xref py py-obj">missing_values</em>
instead.</p>
</dd>
<dt><strong>missing_values</strong><span class="classifier">variable, optional</span></dt><dd><p>The set of strings corresponding to missing data.</p>
</dd>
<dt><strong>filling_values</strong><span class="classifier">variable, optional</span></dt><dd><p>The set of values to be used as default when the data are missing.</p>
</dd>
<dt><strong>usecols</strong><span class="classifier">sequence, optional</span></dt><dd><p>Which columns to read, with 0 being the first.  For example,
<code class="docutils literal notranslate"><span class="pre">usecols</span> <span class="pre">=</span> <span class="pre">(1,</span> <span class="pre">4,</span> <span class="pre">5)</span></code> will extract the 2nd, 5th and 6th columns.</p>
</dd>
<dt><strong>names</strong><span class="classifier">{None, True, str, sequence}, optional</span></dt><dd><p>If <em class="xref py py-obj">names</em> is True, the field names are read from the first line after
the first <em class="xref py py-obj">skip_header</em> lines.  This line can optionally be proceeded
by a comment delimiter. If <em class="xref py py-obj">names</em> is a sequence or a single-string of
comma-separated names, the names will be used to define the field names
in a structured dtype. If <em class="xref py py-obj">names</em> is None, the names of the dtype
fields will be used, if any.</p>
</dd>
<dt><strong>excludelist</strong><span class="classifier">sequence, optional</span></dt><dd><p>A list of names to exclude. This list is appended to the default list
[‘return’,’file’,’print’]. Excluded names are appended an underscore:
for example, <em class="xref py py-obj">file</em> would become <em class="xref py py-obj">file_</em>.</p>
</dd>
<dt><strong>deletechars</strong><span class="classifier">str, optional</span></dt><dd><p>A string combining invalid characters that must be deleted from the
names.</p>
</dd>
<dt><strong>defaultfmt</strong><span class="classifier">str, optional</span></dt><dd><p>A format used to define default field names, such as “f%i” or “f_%02i”.</p>
</dd>
<dt><strong>autostrip</strong><span class="classifier">bool, optional</span></dt><dd><p>Whether to automatically strip white spaces from the variables.</p>
</dd>
<dt><strong>replace_space</strong><span class="classifier">char, optional</span></dt><dd><p>Character(s) used in replacement of white spaces in the variables
names. By default, use a ‘_’.</p>
</dd>
<dt><strong>case_sensitive</strong><span class="classifier">{True, False, ‘upper’, ‘lower’}, optional</span></dt><dd><p>If True, field names are case sensitive.
If False or ‘upper’, field names are converted to upper case.
If ‘lower’, field names are converted to lower case.</p>
</dd>
<dt><strong>unpack</strong><span class="classifier">bool, optional</span></dt><dd><p>If True, the returned array is transposed, so that arguments may be
unpacked using <code class="docutils literal notranslate"><span class="pre">x,</span> <span class="pre">y,</span> <span class="pre">z</span> <span class="pre">=</span> <span class="pre">loadtxt(...)</span></code></p>
</dd>
<dt><strong>usemask</strong><span class="classifier">bool, optional</span></dt><dd><p>If True, return a masked array.
If False, return a regular array.</p>
</dd>
<dt><strong>loose</strong><span class="classifier">bool, optional</span></dt><dd><p>If True, do not raise errors for invalid values.</p>
</dd>
<dt><strong>invalid_raise</strong><span class="classifier">bool, optional</span></dt><dd><p>If True, an exception is raised if an inconsistency is detected in the
number of columns.
If False, a warning is emitted and the offending lines are skipped.</p>
</dd>
<dt><strong>max_rows</strong><span class="classifier">int,  optional</span></dt><dd><p>The maximum number of rows to read. Must not be used with skip_footer
at the same time.  If given, the value must be at least 1. Default is
to read the entire file.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.10.0.</span></p>
</div>
</dd>
<dt><strong>encoding</strong><span class="classifier">str, optional</span></dt><dd><p>Encoding used to decode the inputfile. Does not apply when <em class="xref py py-obj">fname</em> is
a file object.  The special value ‘bytes’ enables backward compatibility
workarounds that ensure that you receive byte arrays when possible
and passes latin1 encoded strings to converters. Override this value to
receive unicode arrays and pass strings as input to converters.  If set
to None the system default is used. The default value is ‘bytes’.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.14.0.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>out</strong><span class="classifier">ndarray</span></dt><dd><p>Data read from the text file. If <em class="xref py py-obj">usemask</em> is True, this is a
masked array.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="numpy.loadtxt.html#numpy.loadtxt" title="numpy.loadtxt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.loadtxt</span></code></a></dt><dd><p>equivalent function when no data is missing.</p>
</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<ul class="simple">
<li><p>When spaces are used as delimiters, or when no delimiter has been given
as input, there should not be any missing data between two fields.</p></li>
<li><p>When the variables are named (either by a flexible dtype or with <em class="xref py py-obj">names</em>,
there must not be any header in the file (else a ValueError
exception is raised).</p></li>
<li><p>Individual values are not stripped of spaces by default.
When using a custom converter, make sure the function does remove spaces.</p></li>
</ul>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r4687f22d1dc9-1"><span class="brackets">1</span></dt>
<dd><p>NumPy User Guide, section <a class="reference external" href="https://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html">I/O with NumPy</a>.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>Comma delimited file with mixed dtype</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="sa">u</span><span class="s2">&quot;1,1.3,abcde&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;myint&#39;</span><span class="p">,</span><span class="s1">&#39;i8&#39;</span><span class="p">),(</span><span class="s1">&#39;myfloat&#39;</span><span class="p">,</span><span class="s1">&#39;f8&#39;</span><span class="p">),</span>
<span class="gp">... </span><span class="p">(</span><span class="s1">&#39;mystring&#39;</span><span class="p">,</span><span class="s1">&#39;S5&#39;</span><span class="p">)],</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">array((1, 1.3, b&#39;abcde&#39;),</span>
<span class="go">      dtype=[(&#39;myint&#39;, &#39;&lt;i8&#39;), (&#39;myfloat&#39;, &#39;&lt;f8&#39;), (&#39;mystring&#39;, &#39;S5&#39;)])</span>
</pre></div>
</div>
<p>Using dtype = None</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># needed for StringIO example only</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="gp">... </span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;myint&#39;</span><span class="p">,</span><span class="s1">&#39;myfloat&#39;</span><span class="p">,</span><span class="s1">&#39;mystring&#39;</span><span class="p">],</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">array((1, 1.3, b&#39;abcde&#39;),</span>
<span class="go">      dtype=[(&#39;myint&#39;, &#39;&lt;i8&#39;), (&#39;myfloat&#39;, &#39;&lt;f8&#39;), (&#39;mystring&#39;, &#39;S5&#39;)])</span>
</pre></div>
</div>
<p>Specifying dtype and names</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s2">&quot;i8,f8,S5&quot;</span><span class="p">,</span>
<span class="gp">... </span><span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;myint&#39;</span><span class="p">,</span><span class="s1">&#39;myfloat&#39;</span><span class="p">,</span><span class="s1">&#39;mystring&#39;</span><span class="p">],</span> <span class="n">delimiter</span><span class="o">=</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">array((1, 1.3, b&#39;abcde&#39;),</span>
<span class="go">      dtype=[(&#39;myint&#39;, &#39;&lt;i8&#39;), (&#39;myfloat&#39;, &#39;&lt;f8&#39;), (&#39;mystring&#39;, &#39;S5&#39;)])</span>
</pre></div>
</div>
<p>An example with fixed-width columns</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="sa">u</span><span class="s2">&quot;11.3abcde&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;intvar&#39;</span><span class="p">,</span><span class="s1">&#39;fltvar&#39;</span><span class="p">,</span><span class="s1">&#39;strvar&#39;</span><span class="p">],</span>
<span class="gp">... </span>    <span class="n">delimiter</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">array((1, 1.3, b&#39;abcde&#39;),</span>
<span class="go">      dtype=[(&#39;intvar&#39;, &#39;&lt;i8&#39;), (&#39;fltvar&#39;, &#39;&lt;f8&#39;), (&#39;strvar&#39;, &#39;S5&#39;)])</span>
</pre></div>
</div>
<p>An example to show comments</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="gp">... </span><span class="s1">text,# of chars</span>
<span class="gp">... </span><span class="s1">hello world,11</span>
<span class="gp">... </span><span class="s1">numpy,5&#39;&#39;&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">genfromtxt</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;S12,S12&#39;</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>
<span class="go">array([(b&#39;text&#39;, b&#39;&#39;), (b&#39;hello world&#39;, b&#39;11&#39;), (b&#39;numpy&#39;, b&#39;5&#39;)],</span>
<span class="go">  dtype=[(&#39;f0&#39;, &#39;S12&#39;), (&#39;f1&#39;, &#39;S12&#39;)])</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>